#!/usr/bin/perl -w
package FillMythMetadata;

#created by Menny Even-Danan (mennyed at the g mail thing)
#additional work by Mark Nolan, Laurie Odgers, cferrier, Lorenzo1985 and paulyche (from Mythbuntu forums).
my $script_version = "1.2.0.4";
#Nov 9th  2010
#http://s.evendanan.net/mythvideo
#http://s.evendanan.net/mythvideo_source

#ideas taken from:
#shabba's script (http://www.skynet.ie/~shabba/tveps.txt). Thread at http://www.gossamer-threads.com/lists/mythtv/users/320471.
#Video length and video thumbs from http://www.perlmonks.org/?node_id=604177. Thanks to MonkE.
# Eli Bendersky's Perl Levenshtein Algorithm implementation - http://eli.thegreenplace.net/programs-and-code/

#This script uses www.thetvdb.com API to retreive information about TV shows.
#This script uses www.themoviedb.org API to retreive information about movies.
#This script uses www.tvsubtitles.net to download tv shows subtitles.

#changelog:
#version 1.2.04
# Issues with mplayer CPU usage, and bad subtitles files.
# re-writing images mechanism
# Issue 83: lowercase show name fix (thanks Matt)

#version 1.2.0.3
# Issues 81,80: fixing wrong video folder query

#version 1.2.0.2
# Issue 79 : fixing duplicate no-medatadata entries

#version 1.2.0.1
# Hash code support

#version 1.2
# Issue 72 - banners jpg file has no name
# Works only on 0.23
# Code refactoring

#version 1.1.0.5
# Issue 71 - File date is taken from the filesystem - by fruitcak.

#version 1.1.0.4
# Issue 70 - Lock file support - by fruitcak.

#version 1.1.0.3
# Issue 67,68 - NFO overriding fix

#version 1.1.0.2
# Issue 67,68 - NFO overriding fix

#version 1.1.0.1
# Issue 66 - posters from tmdb.

#version 1.1.0.0
# Issue 65 - removal of imdb scraping due to 'terms of use' violation.

#version 1.0.9.3
# Issue 60 - Parental Levels - taking data from configuration

#version 1.0.9.2
# Issue 60 - Parental Levels some testing

#version 1.0.9.1
# Issue 61 - quotes in titles and SQL.

#version 1.0.9.0
# General issues with 0.22

#version 1.0.8.8
# Issue 53 - fixing the DVD rip issue.

#version 1.0.8.7
# Much better HTTP caching

#version 1.0.8.6
# Issue 57 - trying to fix the accented characters

#version 1.0.8.5
# Issue 55 - myth-tv version is detected automatically (override is still possible).

#version 1.0.8
# Added TMDB as grabber
# fixed some movies being detected as episodes

#version 1.0.7
#*Issue 47: Taking into account the year of the movie when locating it in IMDB.
#*bug fixes

#version 1.0.6
#*Issue 5:  The season folder should show the season's art-work, and not the series. The series folder should show the series art-work  
#*bug fixes

#version 1.0.5
#*Issue 45:  MythTV 0.22 update for thetvdb.com. Use command arguments:
#	-MYTHTV_022 [0/1]
#		0(default)-Disable featching
#		1-Fetch additional Fanart, Banner, and ScreenShot metadata
#*Issue 43:  Return rating (G, PG, PG-13, R etc) for movies  

#version 1.0.4
#*bug fixes - issue 41

#version 1.0.3 (by cferrier)
#*Motechnet posters retrieval fixed for motechposters.com domain and structure change.
#This includes the neccessary referer spoofing put in to prevent hotlinking.
#*bug fixes

#version 1.0.2
#*Video folder now supports ":" separating.
#*bug fixes

#version 1.0.1
#*Entire folder skip markers - same overriding mechanism for file, has been extended to folder:
#If you wish to override an entire folder, create a file with a name in this format: folder.[folder-name].nfo
#E.g., for folder "MyMovies", create inside this folder a file named "folder.MyMovies.nfo".
#the override options are as file:
#	* To skip a folder completely (just ignore, whatever in the database, if any, is OK): The file should contain '<no_update/>'
#	* To skip a folder parsing, and just add the file with a snapshot to the database (usefull for private videos): The file should contain '<simple_update/>'
#*bug fixes

#version 1.0
#*New IMDB parser. No need for IMDB::Film perl module!
#*New command line arguments:
#	-HOST [hostname] : Override the automatic hostname resolving.
#	-TV_TITLE_TYPE [0/1/2]
#			0-[Filename]
#			1(default)-[Episode number]: [Episode name]
#			2-[Show] S[Season number]E[Episode number] [Episode name]
#			3-[Show] [Season number]x[Episode number] [Episode name]
#	-MOVIE_TITLE_TYPE [0/1/2]
#			0-[Filename]
#			1(default)-[Movie name from IMDB]
#	-THUMB_OFFSET [seconds] : take the video snaphost image from the specified time.
#*Can use an external script to retrieve movies posters.
#*Taking hi-res movie posters from posters.motechnet.com
#*Bug fixes
#

#version 0.9.3
#* New command line argument:
#       * Override Art-Work folder: -ARTFOLDER [full path to folder]
#* DVD folder fixes.
#* bug fixes.

#version 0.9.2
#* Removed dependency of grep's perl regexp implementation.
#* Added support for tv-show format: "ShowName SSEE.avi"
#* bug fixes.

#version 0.9.1
#* Support for DVD rips under VIDEO_TS folder (as MythVideo requires).
#* bug fixes.

#version 0.9
#* It is possible to override the search for a tv show or movie, by creating a file within the video file folder ("nfo" extension)
#which includes the video file info:
#	* TV Show:
#		* To override the series: any .nfo file within the video file folder which contain '<thetvdb seriesid="[ID from thetvdb.com]"/>'
#		* To override the entire video file information: create a file with the same name as the file but with .nfo extension. The file should contain '<thetvdb seriesid="[ID from thetvdb.com]" season="[season number(not ID)]" episode="[episode number(not ID)]"/>'
#		* To override to a specific episode (e.g., a special): create a file with the same name as the file but with .nfo extension. The file should contain '<thetvdb episodeid="[ID from thetvdb.com]"/>'
#	* Movie:
#		* To override the video file: create a file with the same name as the file but with .nfo extension. The file should contain '<imdb id="[ID from imdb.com]"/>'
#	* Skip markers:
#		* To skip a file complete (just ignore, whatever in the database , if any, is OK): create a file with the same name as the file but with .nfo extension. The file should contain '<no_update/>'
#		* To skip a file parsing, and just add the file with a snapshot to the database (usefull for private videos): create a file with the same name as the file but with .nfo extension. The file should contain '<simple_update/>'
#NOTE: NFO files can be hidden.
#* Selects the most similar TV show from all results, rather than the first.
#* New command line argument:
#       * Work on a specific file (can be usefull for replacing imdb.pl script): -F [full path to file]
#       * Change the default snapshot width (height is calcualted by the aspect ratio): -SNAPSHOT_WIDTH [number]
#       * Output episode titles in a short or long format (by paulyche): -SHORT_NAME [1/0]
#		* Change the IP of THE-TV-DB (this should be used rarely!): -TVIP [HOST/IP]
#* SQL password is taken from the MythTV MySQL text file (by Lorenzo1985).
#* bug fixes - thanks to Uter, Lorenzo1985 and paulyche.

#version 0.8
#* Imaegs are saved in the artwork folder - now it is possible to get the images in mythweb! (by Lorenzo1985)
#* images names will no longer have spaces in them (by Lorenzo1985)
#* Redundant DB entries (e.g., video file does not exist) will be removed.
#* Un-needed images (e.g., of deleted video files), will be deleted from the artwork folder

#version 0.7:
#* Moved away from WWW::TV perl module. Now uses www.thetvdb.com API!
#* New command line arguments:
#	* Take snapshots for movies (don't take IMDB's poster) - "0" for take from IMDB.COM, "1" generate snapshot from file: -MOVIE_SNAP [1/0]
#	* Take snapshots for TV shows (don't take The TV DB's poster) - "0" for take from www.thetvdb.com, "1" generate snapshot from file: -TV_SNAP [1/0]
#* major change in images download.
#* bug fixes
#* Known bug: images are not saved in the ART folder, but next to the video files - which means no images in the MythWeb.

#version 0.6:
#* command line support:
#	* Scan specific directory, recursive (overriding the automatic media folder detection): -D [path]
#	* Perform "full" (0)/"new files only" (1) scan (overriding the default specified): -N [1/0]
#	* Perform subtitles download: -S [1/0]
#	* Specify DB password: -DBP [whatever]
#* better IMDB handling... Will take data only if the title is levenshtein similar.
#* bug fixes - including spaces in cover filenames

#version 0.5:
#* support for downloading subtitles - will be downloaded if no subtitle file exists.
#* video snapshot will be created anyway.
#* if file was not found in TV.COM it will be handled as a movie
#* bug fixes

#version 0.4:
#* better posters handling
#* IMDB support
#* Since this script supports TV and Movies, there is no need to specify the folder. It will be taken from the database.
#* No need for extention specification. It will be taken from the database.
#* New filesystem files will be automatically be added to MythVideo database.
#* Bug fixes

#version 0.3:
#* bug fixes

#version 0.2:
#* A bit more logic into the parent folder art.
#* support for [ShowName] [SeasonNumber(in one digit)][EpisodeNumber(two digits]: "My.Show 108.avi"
#* better image naming.

#version 0.1: Initial release

use DBI;
use LWP::UserAgent;
use Sys::Hostname;
use POSIX qw(strftime);
use FillMythMetadataNfoFileReader;
use FillMythMetadataHttpCaller;
use FillMythMetadataUtilities;
use FillMythMetadataFilenameParser;
use FillMythMetadataTMDB;
use FillMythMetadataTheTVDB;
use FillMythMetadataVideoFileUtilities;
use FillMythMetadataSql;
use OpenSubtitlesHashCreator;
use FillMythMetadataParentalRating;
use warnings;
use strict;

#Your SQL server host. If the backend is in this computer, put 'localhost' here.
my $dbhost = 'localhost';

#This is the default setting of MythTV
my $db = 'mythconverg';

#this is the default user of MythTV
my $dbuser = 'mythtv';

#Here you should put the SQL password
my $dbpass = '';

#Set as 1 if you want to scan for new files only, 
#or 0 if you want to get information for all files (even scanned ones)
my $only_new_files = 1;

#Set as 1 it will download picture files, 
#or if set to 0 it will not download any pictures
my $download_picture_files = 1;

#Set as 1 if you want to automatically download subtitles for TV-shows
my $download_subtitles = 0;
my $subtitles_lang = "english";

#Set as 0 if you want to take posters from themoviedb.org, 
#or 1 if you want to generate from file.
#or 2 if you want to take posters from an external script 
my $movies_snapshot = 0;
my $external_poster_command_line = '/usr/share/mythtv/mythvideo/scripts/tmdb.pl -P ';

#Sets the video snapshot width. Height is calculated from the video's aspect ratio.
my $snapshot_width = 320;

#sets the tv show title format:
#you should use the command line argument: -TV_TITLE_TYPE [0/1/2]
#			0-[Filename]
#			1(default)-[Episode number]: [Episode name]
#			2-[Show] S[Season number]E[Episode number] [Episode name]
#			3-[Show] [Season number]x[Episode number] [Episode name]
my $tv_show_title_format = 1;
#sets the movie title format:
#you should use the command line argument: -MOVIE_TITLE_TYPE [0/1/2]
#			0-[Filename]
#			1(default)-[Movie name from themoviedb.org]
my $movie_title_format = 1;

#Sets this value to something higher than 0, if you want the video snapshot to be from a specific time point, 
#else it will be a random point in the video.
my $video_thumb_offset = -1;

# Lock file. Set this to the name of a file to use as a lock file
# so that only one instance of the script is running at a time
my $lock_file = "";

# Force date added to be the same as the creation date of the file
my $force_date_added_to_create_date = 1;

#
#    NOTICE!
#
# Do not touch anything pass this point!
#
my $global_user_agent = FillMythMetadataHttpCaller->get_global_agent();
$global_user_agent->agent("MythVideoFiller/".$script_version);
my $videodirs = "";
my @user_files = ();#empty list to begin with. May it will be overriden with a command line argument

sub download_poster
{
	my $poster_url = $_[0];
	my $target = $_[1];
	
	if ((! -e "$target" || ($only_new_files eq 0)) && ($download_picture_files eq 1))
	{
		return FillMythMetadataHttpCaller::download_poster($poster_url, $target);
	}
	else
	{
		print "Folder art for '$target' already exist.\n";
	}
}


######################################################################################
#
#######################################################


#program start.
my $local_machine_hostname = "";

#checking command line arguments
for (my $arg_index = 0; $arg_index < (1 + $#ARGV); $arg_index++) 
{
	my $arg_type = $ARGV[$arg_index];
	print "looking at '$arg_type'\n";
	if ($arg_type eq "-D")
	{
		$arg_index++;
		$videodirs = $ARGV[$arg_index];
		if ($videodirs !~ /\/$/)
		{
			$videodirs = $videodirs."/";
		}
		print "Overriding DB folder. Will use '$videodirs'\n";
	}
	elsif($arg_type eq "-N")
	{
		$arg_index++;
		if ($ARGV[$arg_index] eq "1")
		{
			$only_new_files = 1;
		}
		else
		{
			$only_new_files = 0;
		}
		print "Overriding new files scan. Will use '$only_new_files'\n";
	}
	elsif($arg_type eq "-P")
	{
		$arg_index++;
		if ($ARGV[$arg_index] eq "1")
		{
			$download_picture_files = 1;
		}
		else
		{
			$download_picture_files = 0;
		}
		print "Overriding download pictures. Will use '$download_picture_files'\n";
	}
	
	elsif($arg_type eq "-S")
	{
		$arg_index++;
		if ($ARGV[$arg_index] eq "1")
		{
			$download_subtitles = 1;
		}
		else
		{
			$download_subtitles = 0;
		}
		print "Overriding subtitles download. Will use '$download_subtitles'\n";
	}
	elsif($arg_type eq "-DBP")
	{
		$arg_index++;
		$dbpass = $ARGV[$arg_index];
		print "Overriding DB password.\n";
	}
	elsif($arg_type eq "-MOVIE_SNAP")
	{
		$arg_index++;
		if ($ARGV[$arg_index] eq '1')
		{
			$movies_snapshot = 1;
		}
		elsif ($ARGV[$arg_index] eq '2')
		{
			$movies_snapshot = 2;
		}
		else
		{
			$movies_snapshot = 0;
		}
		print "Overriding movies snapshot to '$movies_snapshot'.\n";
	}
	elsif($arg_type eq "-SHORT_NAME")
	{
		$arg_index++;
		my $short_tv_name = int($ARGV[$arg_index]);
		if ($short_tv_name eq 0) {$tv_show_title_format = 2;}
		else {$tv_show_title_format = 1;}
		print "Obsolete! use '-TV_TITLE_TYPE' instead!..\n";
	}
	elsif($arg_type eq "-TV_TITLE_TYPE")
	{
		$arg_index++;
		$tv_show_title_format = int($ARGV[$arg_index]);
		print "Overriding TV title name format to '$tv_show_title_format'.\n";
	}
	elsif($arg_type eq "-MOVIE_TITLE_TYPE")
	{
		$arg_index++;
		$movie_title_format = int($ARGV[$arg_index]);
		print "Overriding movie title name format to '$movie_title_format'.\n";
	}
	elsif($arg_type eq "-F")
	{
	        $arg_index++;
	        @user_files = ($ARGV[$arg_index]);
	        print "One file mode. Target file is '$ARGV[$arg_index]'.\n";
	}
	elsif($arg_type eq "-SNAPSHOT_WIDTH")
	{
	        $arg_index++;
	        $snapshot_width = int($ARGV[$arg_index]);
	        print "Snapshot width has been set to $snapshot_width pixels.\n";
	}
	elsif($arg_type eq "-HOSTNAME")
	{
	        $arg_index++;
	        $local_machine_hostname = $ARGV[$arg_index];
	        print "machine hostname is '$local_machine_hostname'.\n";
	}
	elsif($arg_type eq "-THUMB_OFFSET")
	{
	        $arg_index++;
	        $video_thumb_offset = int($ARGV[$arg_index]);
	        print "Snapshot offset will be taken from second '$video_thumb_offset'.\n";
	}
	elsif($arg_type eq "-PROXY")
	{
	        $arg_index++;
			my $proxyadr =$ARGV[$arg_index];;#ip 
			$proxyadr ="http://$proxyadr/"; 
			$global_user_agent->proxy('http', $proxyadr) if length($proxyadr) >4; 
			$global_user_agent->timeout(45); 
	        print "proxy server is '$proxyadr'.\n";
	}
	elsif($arg_type eq "-LOCKFILE")
	{
		$arg_index++;
		print "Lock file is '$lock_file'.\n";
	}
	elsif($arg_type eq "-FORCEDATE")
	{
		$arg_index++;
		if ($ARGV[$arg_index] eq '0')
		{
			$force_date_added_to_create_date = 0;
		}
		else
		{
			$force_date_added_to_create_date = 1;
		}
	}
	else
	{
		print "Fill MythVideo Meta-Data v$script_version\n";
		print "Command line arguments (all these arguments are optionals):\n";
		print "-N [1/0]\n";
		print "			Perform \"full\" (0)/\"new files only\" (1) scan (overriding the default specified)\n";
		print "-P [1/0]\n";
		print "			Download Pictures (0) Skip picture download (1)\n";
		print "-D [path]\n";
		print "			Scan specific directory, recursive (overriding the automatic media folder detection).\n";
		print "-F [full path to file]\n";
		print "			Work on a specific file (can be usefull for replacing imdb.pl script)\n";
		print "-S [1/0]\n";
		print "			Perform subtitles download\n";
		print "-DBP [whatever]\n";
		print "			Specify DB password\n";
		print "-MOVIE_SNAP [0/1/2]\n";
		print "			For movies - generate snapshot from file (1)\n";
		print "			For movies - take snapshot from themoviedb.org (0)\n";
		print "			For movies - take snapshot from external script (command line must be defined) (2)\n";
		print "-TV_SNAP [0/1]\n";
		print "			For TV shows - generate snapshot from file (1), or take it from the-tv-db.com (0)\n";
		print "-SNAPSHOT_WIDTH [number]\n";
		print "			Change the default snapshot width (height is calcualted by the aspect ratio)\n";
		print "-THUMB_OFFSET [seconds]\n";
		print "			Take the video snaphost image from the specified time\n";
		print "-SHORT_NAME [0/1]\n";
		print "			Obsolete. Please use -TV_TITLE_TYPE.\n";
		print "-TV_TITLE_TYPE [0/1/2]\n";
		print "			0-[Filename]\n";
		print "			1(default)-[Episode number]: [Episode name]\n";
		print "			2-[Show] S[Season number]E[Episode number] [Episode name]\n";
		print "			3-[Show] [Season number]x[Episode number] [Episode name]\n";
		print "-MOVIE_TITLE_TYPE [0/1/2]\n";
		print "			0-[Filename]\n";
		print "			1(default)-[Movie name from the-movie-db.org]\n";
		print "-ARTFOLDER [full path to folder]\n";
		print "			Override Art-Work folder. Usefull in case the script fails to detect the correct folder (in multi boxes environment)\n";
		print "-CLEAN_ARTFOLDER [1/0]\n";
		print "			Clean files not referenced in database (1), or leave files alone.\n";
		print "-HOST [hostname]\n";
		print "			Override the automatic hostname resolving.\n";
		print "-PROXY [ip_address:port]\n";
		print "			Use a proxy server eg. '10.1.0.254:3128'.\n";		
		print "-PARENTAL [0/1]\n";
		print "			1(default)-store parental show level \n";
		print "			0-don't handle parental level\n";	
		print "-LOCKFILE [filename]\n";
		print "			Use [filename] as a lock file to ensure only one script at a time\n";
		print "-FORCEDATE [0/1]\n";
		print "			1(default)-force the date added field of metadata to match file creation date\n";
		print "			0-leave date added field as when metadata record created\n";
		print "\n";
		exit;
	}
}

FillMythMetadataUtilities::validate_lock_file($lock_file);

if ((length $local_machine_hostname) < 1)
{
	$local_machine_hostname = hostname();
}
print "Local machine hostname is '$local_machine_hostname'\n";

if ((length $dbpass) < 1) 
{
	if (-e "/etc/mythtv/mysql.txt") 
	{
		print "Using password from mysql.txt\n";
		open( my $R, '<', "/etc/mythtv/mysql.txt" ) or die "couldn't open mysql.txt\n";
		while ( my $line = <$R> ) 
		{
			if ( $line =~ /DBPassword/ ) 
			{
				$dbpass = $line;
			}
		}
		close $R;
		$dbpass =~ s/DBPassword=//g;
		chomp($dbpass);
	}
}
#checking variables
if ((length $dbpass) < 1)
{
        die "Please provide the SQL server password. Use the '-DBP' command argument.\nUse '-h' command argument for more details.\n";
}

if ($only_new_files eq 0)
{
	print "NOTICE: You have selected to update ALL files! This will overwrite all previous data with new data.\n";
}

my $last_series_id = '';#this is saved across files. Will be reset if file parsing is required.
#my @season_information;
my $last_show_name = '';
#this member will be used for show title (actual, from the internet).
my $actual_show_name = '';
my $last_folder_name = '';
my $last_folder_poster = '';
my $content_rating = "NR";
my $thetvdb_imdb_id = '';
my $season_banner= '';
my $banner = '';
my $fanart = '';
my $total_files_count = 0;
my $updated_files = 0;

print "Connecting to database...\n";
my $dbh = DBI->connect("dbi:mysql:$db:$dbhost","$dbuser","$dbpass") || die "Database connection not made: $DBI::errstr";
my $sqlstat;
my $sth;

FillMythMetadataSql::set_db_vars($dbh, $local_machine_hostname);

#querying for Banner folder
my $BannerDirectory = FillMythMetadataSql::get_banners_folder();
print "Banner folder is: $BannerDirectory\n";
if ((length $BannerDirectory gt 0) && (!-e $BannerDirectory))
{
	print "*** Banners folder does not exists!!!!!!!\n";
	die;
}
	
#querying for Fanart folder
my $FanartDirectory = FillMythMetadataSql::get_fanart_folder();
print "Fanart folder is: $FanartDirectory\n";
if ((length $FanartDirectory gt 0) && (!-e $FanartDirectory))
{
		print "*** Fan art folder does not exists!!!!!!!\n";
}
	
#querying for Screenshot folder
my $ScreenshotDirectory = FillMythMetadataSql::get_screenshots_folder();
print "Screenshot folder is: $ScreenshotDirectory\n";
if ((length $ScreenshotDirectory gt 0) && (!-e $ScreenshotDirectory))
{
        print "*** Screenshots folder does not exists!!!!!!!\n";
}

my $CoverartDirectory = FillMythMetadataSql::get_coverart_folder();
print "Cover-art folder is: $CoverartDirectory\n";
if ((length $CoverartDirectory gt 0) && (!-e $CoverartDirectory))
{
        print "*** Cover-art folder does not exists!!!!!!!\n";
}

my @video_folders = ();
if (length $videodirs == 0)
{
	#querying for videos folder
	@video_folders = FillMythMetadataSql::get_videos_folders();
}
else
{
	#it may be ":" separated.
	@video_folders = split(':', $videodirs);
	print "Will scan videos from folder: $videodirs\n";
}

#my $ArtworkDirectory = FillMythMetadataSql::get_screenshots_folder();
#print "Artwork folder is: $ArtworkDirectory\n";

my $video_exts = FillMythMetadataSql::get_video_exts();
print "Extensions: $video_exts\n";

print "Reading list of video files...\n";
my @myfiles = ();
foreach(@video_folders)
{
	my $a_video_folder = FillMythMetadataUtilities::remove_last_sep($_);
	my @video_files = `find -L "$a_video_folder"`;
	foreach(@video_files)
	{
		my $a_file = FillMythMetadataUtilities::trim($_);
		if ((scalar @user_files) eq 0)
		{
			#skiping any file which is not in the file extension list
			if ($a_file !~ m/($video_exts)$/gi)
			{
				#print "file '$a_file' is not in the file extensions list. Skipping.\n";
				next;
			}
			elsif ($a_file =~ m/\/VIDEO_TS.+$/i)
			{
				#print "Skipping DVD rip file...\n";
				next;
			}
			elsif ($a_file =~ m/\/AUDIO_TS.+$/i)
			{
				#print "Skipping DVD rip file (audio files)...\n";
				next;
			}

			my $fixed_a_file = substr($a_file, length($a_video_folder)+1);
			#print "Fixed file: $fixed_a_file  at $a_video_folder\n";
			push(@myfiles, [$a_video_folder, $fixed_a_file]);
		}
		else
		{
			foreach(@user_files)
			{
				my $a_user_file = $_;
				#print "checking '$a_user_file' is the same is  '$a_file'\n";
				if ($a_user_file eq $a_file)
				{
					my $fixed_a_file = substr($a_file, length($a_video_folder)+1);
                        		print "Fixed user file: $fixed_a_file  at $a_video_folder\n";
                        		push(@myfiles, [$a_video_folder, $fixed_a_file]);
				}
			}
		}
	}
}

my $default_show_level = FillMythMetadataParentalRating::init($dbh);

foreach (@myfiles) 
{
	my $isDVD = 0;#issue 53
	my @files_arr = $_;
	my $videodir = $files_arr[0][0];
	my $fullfilename = $files_arr[0][1];
	$fullfilename =~ s/\W*$//gi;
	
	if ($fullfilename =~ m/\/VIDEO_TS$/i)
	{
		$isDVD = 1;#issue 53
		print "DVD root folder...\n";
		$fullfilename =~ s/\/VIDEO_TS$//i;
		print "New fullfilename is $fullfilename\n";
		#next;
	}
	print "======================================================================\n";
	print "File ".$total_files_count." out of ".scalar(@myfiles)."...\n";
	$total_files_count++;
	#print "File: $fullfilename\n";
	#next;
	my($directory, $filename) = $fullfilename =~ m/(.*\/)(.*)$/;
	my $complete_fullfilename = $videodir."/".$fullfilename;
	my $complete_directory = $videodir."/".$directory;
	
	my $video_hash = OpenSubtitlesHashCreator::OpenSubtitlesHash($complete_fullfilename);
	my $current_show_ref = FillMythMetadataSql::get_inetref_for_file($fullfilename, $video_hash);
	
	my $video_has_meta_data = 0;
	
	my $video_length;
	
	if ((!$current_show_ref) || (length $current_show_ref eq 0))
	{
		print "Video file '$filename' does not exist in the DB metadata. Inserting dummy row...\n";
		$video_length = FillMythMetadataVideoFileUtilities::get_video_length($complete_fullfilename);
		#$video_hash = OpenSubtitlesHashCreator::OpenSubtitlesHash($complete_fullfilename);
		FillMythMetadataSql::insert_dummy_row($fullfilename, $filename, $default_show_level, $video_length, $video_hash);
		$video_has_meta_data = 0;
		$current_show_ref = "";
	}
	elsif ($current_show_ref eq "empty")
	{
		$video_has_meta_data = 0;
		$current_show_ref = "";
		print "Video file have an empty metadata row. Trying to get new data.\n";
	}
	else
	{
		print "Video file '$filename' has metadata in the database.\n";
		$video_has_meta_data = 1;
		#till we fix the images stuff, I'll comment out this code
		#my $valid_cover_file = FillMythMetadataSql::is_cover_image_valid($fullfilename);
		#if ($valid_cover_file eq 0)
		#{
		#	#print "The cover of the video file is invalid! I'll try to regenerate it...\n";
		#	$video_has_meta_data = 0;
		#}
	}
	#did the user requested full override?
	if ($only_new_files eq 0)
	{
		$video_has_meta_data = 0;
	}
	
	print "MythTV video folder: $videodir, D=$directory, F=$filename\n";
	#next;
	
	if( $video_has_meta_data eq 0 and $force_date_added_to_create_date eq 1 )
	{
		if (-e $complete_fullfilename)
		{
			my $ctime = (stat($complete_fullfilename))[10];
			my $time_string = strftime "%Y-%m-%d %T", localtime($ctime);
			FillMythMetadataSql::update_file_date($complete_fullfilename, $time_string);
		}
	}

	
	my($series_id, $show, $season, $episode, $episode_id) = ("", "", "", "", "");
	my $imdb_id = "";
	#before anything, I'll check for NFO file
	my $filename_parsing_required = 1;#it will be changed to '0' if I'll find NFO for the file
	my $video_file_with_no_ext;
	
	if ($isDVD eq 1)#issue 53
	{#Ho, it is a DVD in the HD
		#print "Note: DVD in the HD, This is partly supported (filenames with spaces)...\n";
		$filename_parsing_required = 0;
		print "DVD Folder: $directory\n";
		print "DVD Name: $filename\n";
		$video_file_with_no_ext = $filename;
		print "The NFO file will be looked from at '".$complete_directory.$video_file_with_no_ext.".nfo\n";
	}
	else
	{
		($video_file_with_no_ext) = $filename =~ m/(.*)\..*/;
	}
	#support for same filename with nfo ext
	my $video_nfo_file;
	if ($video_file_with_no_ext)#it could be that the file is not a file, but a folder (VIDEO_TS?)
	{
		$video_nfo_file = $complete_directory.$video_file_with_no_ext.".nfo";
		if (!-e $video_nfo_file)
		{
			#support for same filename with nfo ext, but hidden
			$video_nfo_file = $complete_directory.".".$video_file_with_no_ext.".nfo";
			if (!-e $video_nfo_file)
			{
				#maybe entire folder override?
				$video_nfo_file = $complete_directory."folder.nfo";
				#my ($currect_folder_name) = $directory =~ m/^.*\/(.*?)\/$/;
				##could be a file in the root folder
				#if ($currect_folder_name)
				#{
				#	$video_nfo_file = $complete_directory."folder.".$currect_folder_name.".nfo";
				#}
			}
		}
	}	
	
	my $is_tv_show = 0;
		
	#print "looking for '$video_nfo_file' for video file details.\n";
	if (($video_nfo_file) && (-e $video_nfo_file))
	{
		print "Video file has NFO file. Looking for video information in it...\n";
		#my guess is that mostly it will be used for movies, so i'll check it first
		$imdb_id = FillMythMetadataNfoFileReader::locate_imdb_data_in_file($video_nfo_file);
		if (length $imdb_id > 0)
		{
			$is_tv_show = 0;#movie, nota tv show
			$filename_parsing_required = 0;
			if ($imdb_id eq $current_show_ref)
			{
				#print "same imdb ID in the database, and in the NFO file. No need to update\n";
				#leaving the '$video_has_meta_data' as it was (maybe "-N 0" was used)
			}
			else
			{
				print "Found IMDB information in NFO file (imdb id: $imdb_id), using it to get meta-data about the file\n";
				$video_has_meta_data = 0;
			}
		}
		else
		{
			($series_id, $season, $episode, $episode_id) = FillMythMetadataNfoFileReader::locate_thetvdb_data_in_file($video_nfo_file);
			print "Located TV show data in NFO file: Series ID:".$series_id." Season:".$season." Episode:".$episode." Episode ID:".$episode_id."\n";

			if ((length $series_id > 0) && (length $season > 0) && (length $episode > 0))
			{
				$filename_parsing_required = 0;
				$is_tv_show = 1;#a tv show
				my $episode_url = FillMythMetadataTheTVDB::create_episode_url($series_id, $season, $episode);
				if ($current_show_ref eq $episode_url)
				{
					#print "same URL in the database, and in the NFO file. No need to update\n";
					#leaving the '$video_has_meta_data' as it was (maybe "-N 0" was used)
				}
				else
				{
					print "Found THE-TV-DB information in NFO file (series id: $series_id, season: $season, episode: $episode), using it to get meta-data about the file\n";
					$video_has_meta_data = 0;
					$show = "id_".$series_id;
				}
			}
			elsif (length $episode_id > 0)
			{
				$filename_parsing_required = 0;
				$is_tv_show = 1;#a tv show
				my $episode_url = FillMythMetadataTheTVDB::create_episode_url_by_episode_id($episode_id);
				#print "Will get episode data from ".$episode_url."\n";
				if (($video_has_meta_data eq 1) && $current_show_ref eq $episode_url)
				{
					#print "same URL in the database, and in the NFO file. No need to update\n";
					#leaving the '$video_has_meta_data' as it was (maybe "-N 0" was used)
				}
				else
				{
					print "Found THE-TV-DB information in NFO file (episode id: $episode_id). Getting identifies parameters...\n";
					($series_id, $episode_id, $season, $episode) = FillMythMetadataTheTVDB::get_episode_meta_data_by_episode_id($episode_id);
					print "Found THE-TV-DB information in NFO file (series id: $series_id, season: $season, episode: $episode), using it to get meta-data about the file\n";
					$video_has_meta_data = 0;
					$show = "id_".$series_id;
				}
			}
			else
			{
				#maybe skippers
				#they are two types of skipper: enitre data, or just get simple data
				if (FillMythMetadataNfoFileReader::is_skip_file($video_nfo_file))
				{
					$filename_parsing_required = 0;
					print "File have been marked as 'no_update'. Skiping it, what's in the database will stay as is.\n";
					next;
				}
				elsif (FillMythMetadataNfoFileReader::is_simple_meta_data($video_nfo_file))
				{
					$filename_parsing_required = 0;
					print "File have been marked as 'simple_update'. Clearing meta-data, and setting snapshot.\n";
					#getting video length
					if (!$video_length)
					{
						$video_length = FillMythMetadataVideoFileUtilities::get_video_length($complete_fullfilename);
					}
					#video hash
					#if (!$video_hash)
					#{
					#	$video_hash = OpenSubtitlesHashCreator::OpenSubtitlesHash($complete_fullfilename);
					#}
					#creating video snapshot
					my $thumb_path = $filename.".jpg";
					#making the name a bit more friendly...
					$thumb_path =~ s/ /_/gi;
					$thumb_path =~ s/'/_/gi;
					#now adding to the full path - this is done here, so it will be possible to have spaces in the artwork folder
					$thumb_path = $ScreenshotDirectory."/".$thumb_path;
					if (-e $thumb_path)
					{
					        print "Video file already exists. Not over-writing.\n";
					}
					else
					{
						FillMythMetadataVideoFileUtilities::create_video_thumb($complete_fullfilename, $video_length, $thumb_path, $video_thumb_offset, $snapshot_width);
					}
					FillMythMetadataSql::update_simple_metadata($filename, $thumb_path, $fullfilename, $video_length, $video_hash);
					#next file please.
					next;
				}
				else
				{
					print "NFO file is irrelevant.\n";
				}
			}
		}
	}
	
	#now I know if their is a need to get meta-data for the file
	if ($video_has_meta_data eq 1)
	{
		print "No need to get meta-data for file.\n";
		next;
	}
	
	#OK, if we reached here, it means we are going to add metadata about this file
	#into the db. Lets get some general info ready
	#getting video length
	if (!$video_length)
	{
		$video_length = FillMythMetadataVideoFileUtilities::get_video_length($complete_fullfilename);
	}
	#video hash
	#if (!$video_hash)
	#{
	#	$video_hash = OpenSubtitlesHashCreator::OpenSubtitlesHash($complete_fullfilename);
	#}
	
	
	if ($filename_parsing_required == 1)#did not file NFO file
	{
		($show, $season, $episode, $is_tv_show) = FillMythMetadataFilenameParser::parse_tv_show_filename($filename);
	}
	
	if ($is_tv_show eq 1)
	{
		#handling meta-data as TV show
		
		#fixing stuff
		$show =~ tr/[\.\_\-\[\]!\(\)]/ /;
		#to lower-case
		$show =~ tr/[A-Z]/[a-z]/;
		$show = FillMythMetadataUtilities::trim($show);
		my $show_name_for_image = $show;
                #making the name a bit more friendly...
                $show_name_for_image =~ s/\W/_/gi;
		$show_name_for_image = $show_name_for_image;
		#the show image
                my $show_banner_path = $show_name_for_image.".jpg";
                my $show_fanart_path = $show_name_for_image.".jpg";
		#print "Show '$show' actual '$actual_show_name'.\n";
		if ((length $show) eq 0) {exit;}
		$season = FillMythMetadataUtilities::trim($season);
		$episode = FillMythMetadataUtilities::trim($episode);
		#removing leading zeros
		$season =~ s/^0*//;
		$episode =~ s/^0*//;
		#ensuring that the items are not empty
		if ((length $season) eq 0) {$season = "0";}
		if ((length $episode) eq 0) {$episode = "0";}
		print "Found Show: (id: $series_id), '$show', season '$season', episode '$episode'. last ID: $last_series_id\n";
		eval
		{
			#I want to get the meta-data for the file
			#I need the series ID, the season, and the episode.
			#The season and the episode are correct: They either parsed, or retreived from an NFO file
			#The series ID can be NFO parsed one or empty.
			#if it is not empty, then it is the correct one (I mean, I took it from an NFO file)
			#if it is empty, then I can search the current folder for NFO file which contains the series ID
			#series ID was found? Good. Not found: I may want to use the previous series ID.
			#the previous ID is valid ONLY if i'm in the same folder and the series name was not changed!
			#otherwise, I need to look for the series name in THE-TV-DB
			if ((length $series_id) eq 0)
			{
				#I have not detected a special NFO for the file, maybe there is for the show
				#looking for an nfo file inside the folder, maybe it wants to override the data
				#note: this function caches, so it is OK to call it multiple times.
				$series_id = FillMythMetadataNfoFileReader::locate_thetvdb_series_id_in_any_nfo_file($complete_directory);
				#ok, I'll check the last_series_id
				if (($last_folder_name ne $directory) || ($last_show_name ne $show))
				{#moved on, not valid anymore.
					$last_series_id = "";
				}
				
				if ((length $series_id) eq 0)
				{#all my tries had not provided any series ID. I'll take the last (even if it is empty)
					$series_id = $last_series_id;
				}
			}
			print "Getting metadata for '$filename'...\n";
			#note: the series_id may still be empty! But if so, the show name is not empty!!
			if (((length $series_id) eq 0) && ((length $show) eq 0))
			{
				print "FATAL ERROR!! no series_id and no show name!!\n";
				next;
			}
			#OK, everything I need
			#If I'm missing the ID I need to get the ID from the series name
			#If I moved to a new folder, I want to make sure I'm setting the correct folder art
			if (((length $series_id) eq 0) || ($last_folder_name ne $directory))
			{
				if ((length $series_id) eq 0)
				{
						print "Missing series ID. Searching by series name '$show'...\n";
				}
				else
				{
						print "Moved to a new folder. Need to get series meta-data for folder art...\n";
				}
				my $plot;
				$thetvdb_imdb_id="";
				($series_id, $show, $last_folder_poster, $banner, $fanart, $plot, $actual_show_name, $content_rating, $thetvdb_imdb_id) = FillMythMetadataTheTVDB::get_series_meta_data($series_id, $show);
				
				if (length($last_folder_poster) eq 0)
				{
					$last_folder_poster = $banner
				}
				
				#Download Banner
				if ($banner)
				{
					download_poster($banner, $BannerDirectory."/".$show_banner_path);
				}
				#Download Fanart
				if ($fanart)
				{
					download_poster($fanart, $FanartDirectory."/".$show_fanart_path);
				}
				
				if ($last_folder_name ne $directory)
				{
					$last_folder_name = $directory;
					#show have a poster. I want to set the folder's art
					if (length $last_folder_poster > 0)
					{
						print "Downloading folder art for series from '$last_folder_poster' to '$directory'...\n";
						#downloading poster for the series. This is for the parent folder!
						#this folder will get the season poster!
						my $parent_folder = $directory;
						# Skip Season directory will get later
						print "Skipping folder art at directory '$parent_folder'\n";
						if ($parent_folder =~ m/^(\/.+\/).+\/$/) {$parent_folder = $1;}
						while((length $parent_folder) > (length $videodir))
						{
							print "Checking folder art at directory '$parent_folder'\n";
							#there could be a previous folder.jpg/png there
							if (FillMythMetadataUtilities::no_folder_art_at_folder($parent_folder) eq 1)
							{
								download_poster($last_folder_poster, $parent_folder."folder.jpg");
							}
							#now to check my parent
							if ($parent_folder =~ m/^(\/.+\/).+\/$/) {$parent_folder = $1;}
							else {last;}
						}
					}
				}
			}
			#if series_id is empty, then we could not locate the series in the-tv-db
			if (length $series_id eq 0)
			{
				$is_tv_show = 0;
			}
			else
			{
				#at this point, series_id holds the corrrect ID
				$last_series_id = $series_id;
				$last_show_name = $show;
				print "Getting episode information...\n";
				
				my ($episode_name, $description, $rating, $episode_poster, $directors, $year);
				if ((length $episode_id) > 0)#I have the episode ID, I'll use it!
				{#($series_id, $episode_id, $season_number, $episode_number, $episode_name, $plot, $rating, $poster, $director, $air_date)
					my $temp_holder;
					my $temp_imdb_id;
					($temp_holder, $episode_id, $temp_holder, $temp_holder, $episode_name, $description, $rating, $episode_poster, $directors, $year, $temp_imdb_id) = 
						FillMythMetadataTheTVDB::get_episode_meta_data_by_episode_id($episode_id);
					if ($temp_imdb_id)
					{
						$thetvdb_imdb_id = $temp_imdb_id;
					}
					else
					{
						$thetvdb_imdb_id = "";
					}
				}
				else
				{#($series_id, $episode_id, $season_number, $episode_number, $episode_name, $plot, $rating, $poster, $director, $air_date)
					my $temp_holder;
					my $temp_imdb_id;
					($temp_holder, $episode_id, $temp_holder, $temp_holder, $episode_name, $description, $rating, $episode_poster, $directors, $year, $temp_imdb_id) = 
						FillMythMetadataTheTVDB::get_episode_meta_data($series_id, $season, $episode);
					if ($temp_imdb_id)
					{
						$thetvdb_imdb_id = $temp_imdb_id;
					}
					else
					{
						$thetvdb_imdb_id = "";
					}
				}
				print "Found meta-data: series id: $series_id, show: '$show', season: $season, episode: $episode.\n";
				if (length $episode_name > 0)
				{
					$updated_files++;
					
					#print "$episode\n";
					#print "$episode_name\n";
					#print "$actual_show_name\n";
					#print "$season\n";
					#print "$tv_show_title_format\n";
					
					$season_banner = FillMythMetadataTheTVDB::get_season_banners($series_id, $season);
					
					if (! -e $complete_directory."folder.jpg")
					{
						if (length($season_banner) gt 0)
						{
							download_poster($season_banner, $complete_directory."folder.jpg");
						}
						else
						{
							#Use Series poster
							download_poster($last_folder_poster, $complete_directory."folder.jpg");
						}
					}
					
					my $episode_title = FillMythMetadataFilenameParser::format_episode_title($filename, $episode, $episode_name, $actual_show_name, $season, $tv_show_title_format);
					#filename should be in the plot
					$description .= "\n".$filename;
					
					#the video image filename (not fullpath)
					my $thumb_name = $show_name_for_image."-".$season."-".$episode.".jpg";
					my $coverart_path = $thumb_name;
					#the season image
					my $season_poster_path = $show_name_for_image."-".$season.".jpg";

					if (length($season_banner) gt 0)
					{
						#we have a season banner, so we'll use that.
						download_poster($season_banner, $BannerDirectory."/".$season_poster_path);
					}
					else
					{
						#no season banner, then show banner please.
						if (length($last_folder_poster) gt 0)
						{
							download_poster($last_folder_poster, $BannerDirectory."/".$season_poster_path);
						}
						else
						{
							#have no show banner url, so I'll take the screenshot.
							$season_poster_path = "";
						}
					}
					
					if (length $episode_poster > 0)
					{
						print "downloading poster from '$episode_poster'...\n";
						download_poster($episode_poster, $CoverartDirectory."/".$coverart_path, $only_new_files);
					}
					
					print "Creating snapshot for '$fullfilename'...\n";
					FillMythMetadataVideoFileUtilities::create_video_thumb($complete_fullfilename, $video_length, $ScreenshotDirectory."/".$thumb_name, $video_thumb_offset, $snapshot_width);
					
					#making sure that the cover is fine (I mean, it may be bad...)
#					if ((! -e $thumb_path) || ((-s $thumb_path) < 100))
#					{
#						print "The cover is invalid! Using the folder art instead.\n";
#						if (FillMythMetadataUtilities::no_folder_art_at_folder($complete_directory) eq 1)
#						{
#							$thumb_path = "";
#						}
#						else
#						{
#							$thumb_path = $complete_directory."folder.jpg";
#							if (! -e $thumb_path)
#							{
#								$thumb_path = $complete_directory."folder.png";
#								if (! -e $thumb_path)
#								{
#									$thumb_path = $complete_directory."folder.gif";
#									if (! -e $thumb_path)
#									{
#										$thumb_path = "";
#									}
#								}
#							}
#						}
#					}					
					print "found '$episode_title': $description\n";
					my $episode_url_for_inetref = "";
					if (length($thetvdb_imdb_id) > 0)
					{
						$episode_url_for_inetref = $thetvdb_imdb_id;
					}
					else
					{
						$episode_url_for_inetref = FillMythMetadataTheTVDB::create_episode_url_by_episode_id($episode_id);
					}

					#fullfilename
					#inetref
					#title 
					#subtitle
					#season*
					#episode*
					#plot
					#video_length
					#user_rating*
					#directors
					#year*
					#content_rating
					#screenshot_path
					#coverfile_path
					#banner_path
					#fanart_path
					#hash
					FillMythMetadataSql::update_video_metadata($fullfilename, $episode_url_for_inetref, 
						$actual_show_name, $episode_name, $season, $episode,
						$description, $video_length, $rating, $directors, $year, $content_rating,
						$thumb_name, $coverart_path,  $season_poster_path, $show_fanart_path, $video_hash);
					print "'$episode_title' is set!\n";
				}
				else
				{
					print "Did not find meta-data for episode! Will try as a movie...\n";
					$is_tv_show = 0;
				}
			}
			
			if ($download_subtitles eq 1)
			{
				my $full_subtitle_filename = substr($complete_fullfilename, 0, rindex($complete_fullfilename, '.')).".srt";
				print "About to download subtitles for file '$complete_fullfilename'. Target subtitle file is '$full_subtitle_filename'\n";
				download_tv_subtitle($show, $full_subtitle_filename, $season, $episode);
			}
		};
		if($@)
		{
			print "Error while working on file '$filename'! Will try it as a Movie. Error: $@\n";
			$is_tv_show = 0;
		}
	}

	if ($is_tv_show eq 0)#this can be 0 if it was not parsed, or if it could not be found in the TV web-site
	{
		#handling meta-data as movie
		
		#fixing stuff
		
		# Give the filename a more meaningful  name
		my $movie_name = $filename;

                # Enhanced metadata extraction/removal by Laurie Odgers
                # Remove metadata from the filename to get the movie name
                $movie_name =~ s/\.\w+$//; # the extension
                $movie_name =~ s/^\d+(\.|_|-|\s)//; # track number?
                $movie_name =~ s/\[\d{4}\](.*)$//; # sometimes videos include "[2008]", I want to remove EVERYTHING after that
				$movie_name =~ s/[\[,\(,\{]\d{4}[\],\),\}]//; #remove year
                $movie_name =~ tr/[\.\_\-\[\]!\(\)\:]/ /; # turn delimeter characters into spaces
				
                $movie_name =~ s/\s+/ /g; # convert n-whitespaces into a single whitespace
                $movie_name =~ tr/[A-Z]/[a-z]/; # change movie name to lower case
                $movie_name =~ s/\s(xvid|divx|h264|x264|ac3|mpg)(.*)$//g; # remove codecs - and everything after
                $movie_name =~ s/\s(internal|repack|proper|fixed|read nfo|readnfo|unrated|widescreen)(.*)$//g;# remove notes - and everything after
                $movie_name =~ s/\s(dvdrip|screener|hdtv|dsrip|dsr|dvd|bluray|blueray|720p|hr|workprint)(.*)$//g;# remove sources - and everything after

                # Part removal enhancement by Laurie Odgers
                # Remove anything after "part", "cd", "ep" or "webisode"
                $movie_name =~ s/((part|cd|ep|webisode)[\s]*\d+)(.*)$//g;          # Numbering, matches: part 1, cd.1, ep1
                # Roman Numerals, matches: part I, cd.V, webisodeX
                $movie_name =~ s/((part|cd|ep|webisode)[\s]*[i,v,x]+[\s])(.*)$//g;      # Matches "moviename - part i [DivX]"
                $movie_name =~ s/((part|cd|ep|webisode)[\s]*[i,v,x]+$)(.*)$//g; # Matches "moviename - part i"

	
		$movie_name = FillMythMetadataUtilities::trim($movie_name);
		eval
		{
			my $bad_movie = 0;
			#this is a movie?
			my ($imdb_title_url, $imdb_title, $imdb_plot, $imdb_director, $imdb_release_date, $imdb_poster, $imdb_fanart, $imdb_rating, $motechnet_poster);
			$content_rating ="NR";
			if ((length $imdb_id) > 0)
			{
				($imdb_id, $imdb_title_url, $imdb_title, $imdb_plot, $imdb_director, $imdb_release_date, $imdb_poster, $imdb_fanart, $imdb_rating, $motechnet_poster, $content_rating) = 
					FillMythMetadataTMDB::get_movie_meta_data_from_tmdb($imdb_id);
			}
			else
			{
				# get the year released from the filename if possible & 
				my $yearReleased = FillMythMetadataFilenameParser::extract_year_released($filename);
				my $imdb_possible_ID;
				my $tmdb_possible_ID;
				($imdb_possible_ID, $tmdb_possible_ID) = FillMythMetadataTMDB::get_imdb_ID_for_movie_name_tmdb($movie_name, $yearReleased);
				
				if (($imdb_possible_ID) or ($tmdb_possible_ID))
				{
					($imdb_id, $imdb_title_url, $imdb_title, $imdb_plot, $imdb_director, $imdb_release_date, $imdb_poster, $imdb_fanart, $imdb_rating, $motechnet_poster, $content_rating) = 
						FillMythMetadataTMDB::get_movie_meta_data_from_tmdb($imdb_possible_ID, $tmdb_possible_ID);
				}
				else
				{
					print "Can not load movie data for '$movie_name'.\n";
					$bad_movie = 1;
				}
			}
			
			my $img_name = $filename.".jpg";
			#making the name a bit more freindly...
			$img_name =~ s/ /_/gi;
			$img_name =~ s/'/_/gi;
			#now adding to the full path - this is done here, so it will be possible to have spaces in the artwork folder
			my $thumb_path = $ScreenshotDirectory."/".$img_name;
			my $fanart_path = $FanartDirectory."/".$img_name;
			my $poster_path = $CoverartDirectory."/".$img_name;

			FillMythMetadataVideoFileUtilities::create_video_thumb($complete_fullfilename, $video_length, $thumb_path, $video_thumb_offset, $snapshot_width);

			if ($bad_movie == 1)
			{
			
				#fullfilename
				#inetref
				#title 
				#subtitle
				#season*
				#episode*
				#plot
				#video_length
				#user_rating*
				#directors
				#year*
				#content_rating
				#screenshot_path
				#coverfile_path
				#banner_path
				#fanart_path		
				print "Can't find any metadata for this file. Entering dummy info...\n";
				FillMythMetadataSql::update_unknown_video($video_hash, $filename, $img_name);	
				next;
			}
			
			if ($movies_snapshot eq 2) # use external script to retrieve posters
			{
				print "Attempting to get poster using the following command:\n";
				print "$external_poster_command_line $imdb_id\n";
 
				my $poster_url = qx{$external_poster_command_line $imdb_id};
				download_poster($poster_url, $poster_path);
			}
			elsif ($movies_snapshot eq 0) # retrieve posters from the-movie-db
			{
				my $good = 0;
				if ($motechnet_poster)
				{
					print "downloading poster from '$motechnet_poster'...\n";
					$good = download_poster($motechnet_poster, $poster_path); # first download from motechnet
				}
				if ($good eq 0) # if the motechnet download failed
				{
					if ($imdb_poster) # check to see if we have an imdb poster queued up, and if so download it
					{
						print "downloading poster from '$imdb_poster'...\n";
						$good = download_poster($imdb_poster, $poster_path);
					}
					if ($good eq 0) # if all this poster downloading business fails, just create a snapshot and use that
					{
						print "Creating snapshot for '$fullfilename'...\n";
						FillMythMetadataVideoFileUtilities::create_video_thumb($complete_fullfilename, $video_length, $thumb_path, $video_thumb_offset, $snapshot_width);
					}
				}
			}
			else # anything else means just create a thumbnail for the snapshot
			{
				print "Creating snapshot for '$fullfilename'...\n";
				FillMythMetadataVideoFileUtilities::create_video_thumb($complete_fullfilename, $video_length, $thumb_path, $video_thumb_offset, $snapshot_width);
			}
			
			if (($imdb_fanart) && ($fanart_path))
			{
				#download fanart
				download_poster($imdb_fanart, $fanart_path);
			}
			
			#what about the folder?
			my $folder_img_path = $complete_directory."folder.jpg";
			if ((-e $poster_path) && (FillMythMetadataUtilities::no_folder_art_at_folder($complete_directory) eq 1))
			{
				system("cp '$poster_path' '$folder_img_path'");					
			}
			$imdb_plot .= "\n".$filename;
			print "found ($imdb_id) $imdb_title: $imdb_plot\n";
			$imdb_title = FillMythMetadataFilenameParser::format_movie_title($filename, $imdb_title, $movie_title_format);

			#fullfilename
			#inetref
			#title 
			#subtitle
			#season*
			#episode*
			#plot
			#video_length
			#user_rating*
			#directors
			#year*
			#content_rating
			#screenshot_path
			#coverfile_path
			#banner_path
			#fanart_path
			FillMythMetadataSql::update_video_metadata($fullfilename, $imdb_id, 
				$imdb_title, "", 0, 0,
				$imdb_plot, $video_length, $imdb_rating, $imdb_director, $imdb_release_date, $content_rating,
				$img_name, $img_name, $img_name, $img_name, $video_hash);
				
			print "'$imdb_title' is set!\n";
			$updated_files++;
		};
		if ($@)
		{
			print "Error while working on file '$filename'! Error: $@\n";
		}
	}
}
print "======================================================================\n";
print "Finished going over all the files: Found $total_files_count, updated $updated_files files.\n";
my $files_count_for_deletion = 0;
print "Going over all video files in the database, and deleting any redundant entries (i.e., video files no longer exist)...\n";
$sqlstat=qq {select filename from videometadata};
$sth = $dbh->prepare($sqlstat);
$sth->execute();
my $video_file_name;
$sth->bind_columns( undef, \$video_file_name );
my @files_to_delete;
while ($sth->fetch())
{
	my $video_file_exists = 0;
	if (($video_file_name) && (length $video_file_name > 0))
	{
		# || (! -e $video_file_name))
		foreach(@video_folders)
		{
			my $a_video_folder = FillMythMetadataUtilities::remove_last_sep($_);
			my $possible_video_file = $a_video_folder."/".$video_file_name;
			#print "checking if $possible_video_file exists...\n";
			if (-e $a_video_folder."/".$video_file_name)
			{
				$video_file_exists = 1;
			}
		}
		if ($video_file_exists eq 0)
		{
			print "Video file '$video_file_name' does not exist in filsystem! Will be removed from DB...\n";
			push(@files_to_delete, $video_file_name);
			$files_count_for_deletion++;
		}
		else
		{
			#print "Video file '$video_file_name' exists in filsystem.\n";
		}
	}
}

foreach(@files_to_delete)
{
	my $file_to_delete = $_;
	print "Deleting '$file_to_delete' from database...\n";
	my $sql_file_to_delete = FillMythMetadataSql::ensure_string_is_valid_for_SQL($file_to_delete);
	$sqlstat=qq {delete from videometadata where filename="$sql_file_to_delete"};
	$sth = $dbh->prepare($sqlstat);
	$sth->execute();
}
print "Deleted $files_count_for_deletion entries from database!\n";

print "======================================================================\n";
my $cleanArtFolder = 0;
if ($cleanArtFolder eq 1)
{
	print "Going over all image files in the artwork folder (at '$ScreenshotDirectory'), and deleting any redundant image files (i.e., owner video file does not exist)...\n";
	$files_count_for_deletion = 0;

	my $image_files_path = $ScreenshotDirectory."/*.jpg";
	my @image_files = glob($image_files_path);

	foreach (@image_files) 
	{
		my $full_image_filename = $_;
		#print "Image: '$full_image_filename'\n";
		eval
		{
			my $sql_full_image_filename = FillMythMetadataSql::ensure_string_is_valid_for_SQL($full_image_filename);
			$sqlstat=qq {select coverfile from videometadata where coverfile="$sql_full_image_filename" OR screenshot="$sql_full_image_filename"};
			$sth = $dbh->prepare($sqlstat);
			$sth->execute();

			my $cover_file;
			$sth->bind_columns( undef, \$cover_file );
			my $video_exists = 0;
			if (!$sth->fetch())
			{
				$video_exists = 0;		
			}
			elsif (($cover_file) && (length $cover_file > 0))
			{
				$video_exists = 1;
			}
			else
			{
				$video_exists = 0;
			}
			
			if ($video_exists eq 1)
			{
				#print "'$full_image_filename' is a cover for a video. It will not be deleted.\n";
			}
			else
			{
				print "Image '$full_image_filename' does not exist in the DB metadata. deleting the file...\n";
				unlink($full_image_filename);
				$files_count_for_deletion++;
			}
		};
		if ($@)
		{
			print "Error while working on file '$full_image_filename'! Error: $@\n";
		}
	}
	print "Deleted $files_count_for_deletion redundant image files from the artwork folder.\n";

	print "======================================================================\n";
}
print "Completing database...\n";
$sth->finish();
$dbh->disconnect();
print "======================================================================\n";
print "Thanks for using MythVideo Metadata Updater v$script_version.\n";
print "======================================================================\n";
FillMythMetadataUtilities::release_lock_file();
